package com.JackTang.fruit.servlets;

import com.JackTang.fruit.dao.FruitDAO;
import com.JackTang.fruit.dao.impl.FruitDAOImpl;
import com.JackTang.fruit.pojo.Fruit;
import com.myssm.myspringmvc.ViewBaseServlet;
import com.myssm.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Jack Tang
 * @description
 * @create 2023-05-05 19:41
 */
@WebServlet("/update.do")
public class UpdateServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置编码
        req.setCharacterEncoding("utf-8");

        // 获取参数
        String fidStr = req.getParameter("fid");
        int fid = Integer.parseInt(fidStr);
        String fname = req.getParameter("fname");
        String priceStr = req.getParameter("price");
        int price = Integer.parseInt(priceStr);
        String fcountStr = req.getParameter("fcount");
        int fcount = Integer.parseInt(fcountStr);
        String remark = req.getParameter("remark");

        // 更新
        fruitDAO.updateFruit(new Fruit(fid,fname,price,fcount,remark));

        // 资源跳转
        //super.processTemplate("index",req,resp);
        // 相当于 req.getRequestDispatcher("index.html").forward(req,resp);
        // 此处需要重定向，目的就是重新给Indexservlet发请求，重新获取fruitList，然后覆盖到session中，
        //这样index.html页面上显示的session中的数据才是最新的
        resp.sendRedirect("index");
    }
}